# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     city
   Description :
   Author :       Flyoung
   date：          2023/9/5
-------------------------------------------------
   Change Activity:
                   2023/9/5:
-------------------------------------------------
"""
from typing import AnyStr

from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

from db.common import with_session

Base = declarative_base()


class City(Base):
    __tablename__ = 'city'

    city_id = Column(Integer, primary_key=True, autoincrement=True)
    city_name = Column(String(255), nullable=True)
    city_score = Column(Float, nullable=True)

    def __init__(self, city_name, city_score):
        self.city_name = city_name
        self.city_score = city_score

    def to_dict(self):
        return {
            'city_id': self.city_id,
            'city_name': self.city_name,
            'city_score': self.city_score
        }


@with_session
def get_all_city(session: Session):
    return [i.to_dict() for i in session.query(City).all()]


@with_session
def add_city(session: Session, city_name: AnyStr, city_score=0):
    city = City(city_name, city_score)
    session.add(city)
    return city
